var contactBar = null;

document.ready(function() {
    contactBar = getHtml('writeBar1');
    setHtml('writeBar1', '');

    queryKey('ok');
});

function queryKey(flag) {
    openLoad();

    var param = {};
    param.ctrl = 'customer';
    param.func = 'getContactList';
    param.type0 = getAjaxValue('selectList1');
    param.isuse = getAjaxValue('selectList2');
    param.keyword = getAjaxValue('keyword1');

    sendAjax(param, function(result) {
        contact.list = result.contactList;
        setHtml('listCount', result.listCount);

        setDisplay('mainBg', '');
        closeLoad();
    });
}

function addContact() {
    setValue('tempid', '');
    setValue('flag', 'add');
    openFloat('600px', 'auto', '70px', lyFloatLeft, contactBar);
    setHtml('writeTitle1', '添加客服');
    setUpload();
}

function closeWriteDiv() {
    closeFloat();
}

function setUpload() {
    layui.use('upload', function() {
        var upload = layui.upload;
        var uploadInst1 = upload.render({
            elem: '#selectImg1', 
            accept: 'images', 
            acceptMime: 'image/*', 
            auto: false, 
            size: 500, 
            choose: function(obj) {
                obj.preview(function(index, file, result) {
                    getItem('headpic').src = result;
                    files1 = result;
                });
            }
        });

        var uploadInst2 = upload.render({
            elem: '#selectImg2', 
            accept: 'images', 
            acceptMime: 'image/*', 
            auto: false, 
            size: 500, 
            choose: function(obj) {
                obj.preview(function(index, file, result) {
                    getItem('qrcodepic').src = result;
                    files2 = result;
                });
            }
        });
    });
}

function editContact(id) {
    setValue('tempid', id);
    setValue('flag', 'edit');

    var param = {};
    param.ctrl = 'customer';
    param.func = 'getContactInfo';
    param.kferid = getAjaxValue('tempid');

    sendAjax(param, function(result) {
        openFloat('600px', 'auto', '70px', lyFloatLeft, contactBar);
        setHtml('writeTitle1', '编辑客服');
        setUpload();

        var info = result.contactInfo;

        setValue('name', info.name);
        setValue('introduce', info.introduce);
        setValue('phone', info.phone);
        setValue('sx', info.sx);

        locationSelect('type', info.type);

        if (info.pic != null && info.pic != '') {
            files1 = info.pic;
            getItem('headpic').src = info.pic;
        }
        if (info.qrcode != null && info.qrcode != '') {
            files2 = info.qrcode;
            getItem('qrcodepic').src = info.qrcode;
        }

        if (info.isuse == 1)
            getItemByName('contactstatus', 0).checked = 'checked';
        else
            getItemByName('contactstatus', 1).checked = 'checked';
    });
}

function deleteContact(id, name) {
    setValue('tempid', id);
    openConfirm('确定删除 【' + name + '】 吗？', tipleft, function() {
        var param = {};
        param.ctrl = 'customer';
        param.func = 'deleteContact';
        param.kferid = getAjaxValue('tempid');

        sendAjax(param, function(result) {
            queryKey('ok');
        });
    });
}

function saveContact() {
    var list = [
        {id:'name', name:'姓名', rule:'wick', empty:false}, 
        {id:'introduce', name:'简介', rule:'wick', empty:false}, 
        {id:'phone', name:'联系电话', rule:'contact', empty:false}, 
        {id:'type', name:'类型', rule:'', empty:false}, 
        {id:'sx', name:'顺序', rule:'num', empty:false}];
    if (checkParam(list)) {
        return;
    }

    if (files1 == '') {
        showTip('请上传客服头像');
        return;
    }
    if (files2 == '') {
        showTip('请上传微信二维码');
        return;
    }

    var param = {};
    param.ctrl = 'customer';
    param.func = 'saveContact';
    param.flag = getAjaxValue('flag');
    param.kferid = getAjaxValue('tempid');
    param.name = getAjaxValue('name');
    param.introduce = getAjaxValue('introduce');
    param.phone = getAjaxValue('phone');
    param.type0 = getAjaxValue('type');
    param.sx = getAjaxValue('sx');
    param.isuse = getItemByName('contactstatus').checked ? '1' : '0';
    param.headpic = encodeURIComponent(files1);
    param.qrcodepic = encodeURIComponent(files2);

    sendAjax(param, function(result) {
        closeFloat();
        queryKey('ok');
    });
}